package com.plugins.pluginlib.utils;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 图片处理工具
 */
public class ImageUtil {
    /**
     * 保存图标
     * @param path
     * @param drawable
     * @return
     */
    public static String saveIcon(String path,Drawable drawable){
        File file = new File(path);
        if(!file.exists()){
            Bitmap bitmap = drawableToBitmap(drawable);
            try {
                FileOutputStream fout = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fout);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bos.flush();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    /**
     * drawable to bitmap
     * @param drawable
     * @return
     */
    public static Bitmap drawableToBitmap(Drawable drawable){
        BitmapDrawable bd = (BitmapDrawable) drawable;
        Bitmap bm= bd.getBitmap();
        return bm;
    }
}

